package com.jb.repair.fragment;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.jb.repair.R;
import com.jb.repair.acticity.CheckLocActivity;
import com.jb.repair.acticity.ShowBugActivity;
import com.jb.repair.acticity.ShowChangeActivity;
import com.jb.repair.common.Constants;
import com.jb.repair.dao.QueryDb;
import com.jb.repair.model.QxjlModel;
import com.jb.repair.model.TaskModel;
import com.jb.repair.model.YcjlModel;

import java.util.ArrayList;
import java.util.List;

public class CheckFragment extends Fragment implements View.OnClickListener {

    private TextView tvChange, tv_name;
    private TextView tvBug;
    private ListView lvNow;
    private ListView lvBefore;
    private List<TaskModel> todayTaskModelList;
    private List<YcjlModel> abnormalList = new ArrayList<>();
    private List<QxjlModel> defectList = new ArrayList<>();
    private String dis = " ";

    private ToggleButton tb;
    private TextView tv_message;
    private Boolean hasB = false;

    SharedPreferences sp;
    SharedPreferences sp1;
    SharedPreferences sp2;
    int flag = 1;

    public CheckFragment() {
    }

    @Override
    public void onResume() {
        super.onResume();
        abnormalList.clear();
        defectList.clear();
        tb.setChecked(hasB);
        for (int m = 0; m < todayTaskModelList.size(); m++) {
            abnormalList.addAll(QueryDb.getInstance(getActivity()).getAllAbnormal(todayTaskModelList.get(m).guid));
            defectList.addAll(QueryDb.getInstance(getActivity()).getAllDefect(todayTaskModelList.get(m).guid));
            lvBefore.setAdapter(new Lv3BeforAdapter());
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.fragment_check2, null);
        sp = getActivity().getSharedPreferences(Constants.SP_NAME, getActivity().MODE_PRIVATE);
        sp1 = getActivity().getSharedPreferences("check.txt", getActivity().MODE_PRIVATE);
        sp2 = getActivity().getSharedPreferences("changable.txt", getActivity().MODE_PRIVATE);
        initview(view);
        todayTaskModelList = QueryDb.getInstance(getActivity()).getTodayTask(false);
        tvChange.setOnClickListener(this);
        tvBug.setOnClickListener(this);

        lvNow.setAdapter(new LvNowAdapter());
        tvChange.setBackgroundResource(R.drawable.textview_selected);
        return view;
    }

    private void initview(View view) {
        tv_message = (TextView) view.findViewById(R.id.tv_message);
        tb = (ToggleButton) view.findViewById(R.id.tb);
        tv_name = (TextView) view.findViewById(R.id.tv_name);
        tvChange = (TextView) view.findViewById(R.id.tv_change);
        tv_name.setText(sp.getString(Constants.PERSON_NAME, ""));
        tvBug = (TextView) view.findViewById(R.id.tv_bug);
        lvNow = (ListView) view.findViewById(R.id.lv_now);
        lvBefore = (ListView) view.findViewById(R.id.lv_before);

        tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                todayTaskModelList = QueryDb.getInstance(getActivity()).getTodayTask(isChecked);
                lvNow.setAdapter(new LvNowAdapter());
                abnormalList.clear();
                defectList.clear();
                hasB = isChecked;
                for (int m = 0; m < todayTaskModelList.size(); m++) {
                    abnormalList.addAll(QueryDb.getInstance(getActivity()).getAllAbnormal(todayTaskModelList.get(m).guid));
                    defectList.addAll(QueryDb.getInstance(getActivity()).getAllDefect(todayTaskModelList.get(m).guid));
                    lvBefore.setAdapter(new Lv3BeforAdapter());
                }
                if (flag == 1)
                    lvBefore.setAdapter(new Lv3BeforAdapter());
                else
                    lvBefore.setAdapter(new Lv2BeforAdapter());
            }
        });


        lvNow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position > 0) {
                    Intent intent = new Intent(getActivity(), CheckLocActivity.class);
                    CheckLocActivity.task_id = todayTaskModelList.get(position - 1).guid;
//                    intent.putExtra("changable",true);
                    sp2.edit().putBoolean("changable", true).commit();
                    startActivity(intent);
                }
            }
        });
        lvBefore.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (flag == 1) {
                    Intent excepintent = new Intent(getActivity(), ShowChangeActivity.class);
                    excepintent.putExtra("ycjl", abnormalList.get(position - 1));
                    startActivity(excepintent);
                }
                if (flag == 2) {
                    Intent bugintent = new Intent(getActivity(), ShowBugActivity.class);
                    bugintent.putExtra("qxjl", defectList.get(position - 1));
                    startActivity(bugintent);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_change:
                flag = 1;
                tvBug.setBackgroundResource(R.drawable.textview_unselet);
                tvChange.setBackgroundResource(R.drawable.textview_selected);
                if (abnormalList.size() == 0) {
                    tv_message.setVisibility(View.VISIBLE);
                    tv_message.setText("暂无异常记录");
                    lvBefore.setVisibility(View.GONE);
                } else {
                    tv_message.setVisibility(View.GONE);
                    lvBefore.setVisibility(View.VISIBLE);
                    sp1.edit().putBoolean("history", false).commit();
                    sp1.edit().putBoolean("change", true).commit();
                    sp1.edit().putBoolean("bug", false).commit();
                    lvBefore.setAdapter(new Lv3BeforAdapter());
                }
                break;
            case R.id.tv_bug:
                flag = 2;
                tvBug.setBackgroundResource(R.drawable.textview_selected);
                tvChange.setBackgroundResource(R.drawable.textview_unselet);
                if (defectList.size() == 0) {
                    tv_message.setVisibility(View.VISIBLE);
                    tv_message.setText("暂无缺陷记录");
                    lvBefore.setVisibility(View.GONE);
                } else {
                    tv_message.setVisibility(View.GONE);
                    lvBefore.setVisibility(View.VISIBLE);
//                lvNow.setAdapter(new Lv2NowAdapter());
                    lvBefore.setAdapter(new Lv2BeforAdapter());
                }
                break;

        }
    }

    class LvNowAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return (todayTaskModelList.size() + 1);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = null;
            ViewHolder viewHolder;
            if (convertView == null) {
                v = View.inflate(getActivity(), R.layout.lv_check, null);
                viewHolder = new ViewHolder();
                viewHolder.tv1 = (TextView) v.findViewById(R.id.tv1);
                viewHolder.tv2 = (TextView) v.findViewById(R.id.tv2);
                viewHolder.tv3 = (TextView) v.findViewById(R.id.tv3);
                viewHolder.tv4 = (TextView) v.findViewById(R.id.tv4);
                viewHolder.tv5 = (TextView) v.findViewById(R.id.tv5);
                viewHolder.tv6 = (TextView) v.findViewById(R.id.tv6);
                v.setTag(viewHolder);
            } else {
                v = convertView;
                viewHolder = (ViewHolder) v.getTag();
            }
            if (position == 0) {
                viewHolder.tv1.setText("");
                viewHolder.tv2.setText("日期");
                viewHolder.tv3.setText("任务");
                viewHolder.tv4.setText("设备总数");
                viewHolder.tv5.setText("待点检项目总数");
                viewHolder.tv6.setVisibility(View.INVISIBLE);
            } else {
                TaskModel news = todayTaskModelList.get(position - 1);
                viewHolder.tv1.setText(position + "");
                viewHolder.tv2.setText(news.task_date.substring(0, 10));
                if (news.isB)
                    viewHolder.tv3.setText(news.task + "(B角)");
                else
                    viewHolder.tv3.setText(news.task);
                viewHolder.tv4.setText(news.total_dev_num + "");
                viewHolder.tv5.setText(news.total_unsi_num + "");
            }
            return v;
        }

        class ViewHolder {
            TextView tv1;
            TextView tv2;
            TextView tv3;
            TextView tv4;
            TextView tv5;
            TextView tv6;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }

    class LvBeforAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = null;
            ViewHolder viewHolder;
            if (convertView == null) {
                v = View.inflate(getActivity(), R.layout.lv_check2, null);
                viewHolder = new ViewHolder();
                viewHolder.tv1 = (TextView) v.findViewById(R.id.tv1);
                viewHolder.tv2 = (TextView) v.findViewById(R.id.tv2);
                viewHolder.tv3 = (TextView) v.findViewById(R.id.tv3);
                viewHolder.tv4 = (TextView) v.findViewById(R.id.tv4);
                viewHolder.tv5 = (TextView) v.findViewById(R.id.tv5);
                viewHolder.tv6 = (TextView) v.findViewById(R.id.tv6);
                v.setTag(viewHolder);
            } else {
                v = convertView;
                viewHolder = (ViewHolder) v.getTag();
            }
            if (position == 0) {
                viewHolder.tv1.setText("");
                viewHolder.tv2.setText("点检日期");
                viewHolder.tv3.setText("点检任务");
                viewHolder.tv4.setText("异常项目数");
                viewHolder.tv5.setText("点检到位率");
                viewHolder.tv6.setVisibility(View.INVISIBLE);
            } else {
            }
            return v;
        }

        class ViewHolder {
            TextView tv1;
            TextView tv2;
            TextView tv3;
            TextView tv4;
            TextView tv5;
            TextView tv6;
        }

    }

    class Lv2BeforAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return (defectList.size() + 1);
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = null;
            ViewHolder viewHolder;
            if (convertView == null) {
                v = View.inflate(getActivity(), R.layout.lv_check3, null);
                viewHolder = new ViewHolder();
                viewHolder.tv1 = (TextView) v.findViewById(R.id.tv1);
                viewHolder.tv2 = (TextView) v.findViewById(R.id.tv2);
                viewHolder.tv3 = (TextView) v.findViewById(R.id.tv3);
                viewHolder.tv4 = (TextView) v.findViewById(R.id.tv4);
                viewHolder.tv5 = (TextView) v.findViewById(R.id.tv5);
                v.setTag(viewHolder);
            } else {
                v = convertView;
                viewHolder = (ViewHolder) v.getTag();
            }
            if (position == 0) {
                viewHolder.tv1.setText("");
                viewHolder.tv2.setText("缺陷发生时间");
                viewHolder.tv3.setText("缺陷等级");
                viewHolder.tv4.setText("缺陷说明");
                viewHolder.tv5.setVisibility(View.INVISIBLE);
            } else {
                QxjlModel news = defectList.get(position - 1);
                viewHolder.tv1.setText(position + "");
                viewHolder.tv2.setText(news.found_time);
                String level = QueryDb.getInstance(getActivity()).getName("CODE_NAME", "TB_SYS_CODE", "CODE", news.level_id);
                viewHolder.tv3.setText(level);
                dis = news.defect_describe;
                viewHolder.tv4.setText(dis);
            }
            return v;
        }

        class ViewHolder {
            TextView tv1;
            TextView tv2;
            TextView tv3;
            TextView tv4;
            TextView tv5;
        }

    }

    class Lv3BeforAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return (abnormalList.size() + 1);
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = null;
            ViewHolder viewHolder;
            if (convertView == null) {
                v = View.inflate(getActivity(), R.layout.lv_check3, null);
                viewHolder = new ViewHolder();
                viewHolder.tv1 = (TextView) v.findViewById(R.id.tv1);
                viewHolder.tv2 = (TextView) v.findViewById(R.id.tv2);
                viewHolder.tv3 = (TextView) v.findViewById(R.id.tv3);
                viewHolder.tv4 = (TextView) v.findViewById(R.id.tv4);
                viewHolder.tv5 = (TextView) v.findViewById(R.id.tv5);
                v.setTag(viewHolder);
            } else {
                v = convertView;
                viewHolder = (ViewHolder) v.getTag();
            }
            if (position == 0) {
                viewHolder.tv1.setText("");
                viewHolder.tv2.setText("异常发生时间");
                viewHolder.tv3.setText("处理方式");
                viewHolder.tv4.setText("异常说明");
                viewHolder.tv5.setVisibility(View.INVISIBLE);
            } else {
                YcjlModel news = abnormalList.get(position - 1);
                viewHolder.tv1.setText(position + "");
                viewHolder.tv2.setText(news.found_time);

                viewHolder.tv3.setText(news.dealtype);
                viewHolder.tv4.setText(news.abn_describe + "");
            }
            return v;
        }

        class ViewHolder {
            TextView tv1;
            TextView tv2;
            TextView tv3;
            TextView tv4;
            TextView tv5;
        }

    }
}